var userImgs;
var Index;
var voiceTime;
var figureTime;
var flagTime=0;
var voiceurl;
Page({
  data:{
    fail:"none",
    question:"",
    introduce:"",
    givemoney:"￥0",
    answers:[],
    zans:0,
    userImg:"",
    NoshowQue: 'display',
    showQue: 'none',
    niming:"匿名用户"
  },
  lookUserImg:function(e){
    var index=e.currentTarget.id;
    var path=[];
    path.push(this.data.userImage[index]);
    for(var i=0;i<this.data.userImage[index].length;i++)
    if(i!=index)
    path.push(this.data.userImage[i]);
    wx.previewImage({
      urls: path,
    })
  },
  question:function(ID){
    var that=this;
    wx.request({
      url: "https://www.liuxuanlong.xyz/fenda/wx/findQuestionByIdOrUuName",
      data:{
        id:ID
      },
      method:"GET",
      success:function(res){
        console.log(res.data);
        if(res.data.isAnonymous!='true')
        that.setData(
          {
            niming:res.data.isAnonymous
          }
        );
        userImgs=res.data.questionImgUrl;
        for(var i=0;i<res.data.answerList.length;i++){
          if(res.data.answerList[i].info.length==0)
          res.data.answerList[i].textDisplay='none';
          else
          res.data.answerList[i].textDisplay="display";
          if(res.data.answerList[i].answerImgSet.length==0)
          res.data.answerList[i].imageDisplay='none';
          else{
          res.data.answerList[i].imageDisplay='display';
          
          }
          if(res.data.answerList[i].recordUrl.length==0)
          res.data.answerList[i].recordDisplay='none';
          else
          res.data.answerList[i].recordDisplay='display';
          res.data.answerList[i].f5="点击播放";
          var timeNow=Date.now();
          var time=res.data.answerList[i].answerTime;
          time=timeNow-time;
          time=parseInt(time/60000);
          if(time>60){
            if(time>1440)
            time=parseInt(time/1440)+'天';
            else
            time=parseInt(time/60)+'小时';
          }
          else{
            time=time+'分钟'
          }
          res.data.answerList[i].answerTime=time;

        }
        that.setData(
          {
            question:res.data.info,
            userImage:res.data.questionImgUrl,
            answers:res.data.answerList,
            NoshowQue: 'none',
            showQue: 'display'
          }
        );
        
      },
      fail:function(){
        that.setData(
          {
            fail:"display"
          }
        );
      },
    })
  },
  prewExpertImg:function(e){
    var index1=e.currentTarget.id;
    var index2=e.target.dataset.index;
    var path=[];
    path.push(this.data.answers[index1].answerImgSet[index2].imgUrl);
    for(var i=0;i<this.data.answers[index1].answerImgSet.length;i++){
      if(i!=index2)
      path.push(this.data.answers[index1].answerImgSet[i].imgUrl);
    }
    wx.previewImage({
      urls: path,
    }) 
   
  },
  prewImg:function(e){
    var index=e.currentTarget.id;
    
    var url=this.data.answers[index].answerImgSet;
    
    var imgUrl=[];
    for(var i=0;i<url.length;i++)
    imgUrl.push(url[i].imgUrl)
   
    wx.previewImage({
      urls:imgUrl,
    })
  },
  listen: function (e) {

    var index = e.currentTarget.id;
    Index = index;
    var answer = this.data.answers;
    voiceTime = answer[index].recordTime;
    var url = this.data.answers[index].recordUrl;
    var that = this;
    var font = answer[index].f5;



    for (var i = 0; i < answer.length; i++) {
      if (i != index) {
        answer[i].f5 = "点击播放";
        that.setData(
          {
            answers: answer
          }
        );
      }
    }
    if (font == "点击播放")
     {
       answer[index].f5='缓冲中...';
       that.setData(
         {
           answers:answer
         }
       );
      wx.downloadFile({
        url: url,
        success: function (res) {
          voiceurl = res.tempFilePath;
          figureTime = setInterval(that.figureTime, 500);
          answer[index].f5 = "播放中...";
          that.setData(
            {
              answers: answer
            }
          );
          wx.playVoice({
            filePath: voiceurl,
            
          })
          
        }
      })
  }
    if (font == "播放中...") {
      wx.stopVoice();
      clearInterval(figureTime);
      flagTime = 0;
      answer[index].f5 = "点击播放";
      that.setData(
        {
          answers: answer
        }
      );
    }
    /*if(font=="已暂停播放"){
      wx.playVoice({
        filePath: voiceurl,
      })
      figureTime=setInterval(that.figureTime,500);
      answer[index].f5="播放中...";
      that.setData(
        {
          answers:answer
        }
      );
    }*/
  },
  figureTime: function () {
    flagTime = flagTime + 0.5;
    if (flagTime >= voiceTime) {
      this.data.answers[Index].f5 = "点击播放";
      flagTime = 0;
      clearInterval(figureTime);
      wx.removeSavedFile({
        filePath: voiceurl,
      })
      this.setData(
        {
          answers: this.data.answers
        }
      );
    }
  },
  exmine:function(){
    wx.previewImage({
      // current: 'String', // 当前显示图片的链接，不填则默认为 urls 的第一张
      urls: userImgs,
      success: function(res){
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  zan:function(){
    var zan=this.data.zans;
    zan++;
    this.setData(
      {
        zans:zan
      }
    );
  },
  nowarns:function(){
    this.setData(
      {
        fail:"none"
      }
    );
  },
  onLoad:function(options){
    // 生命周期函数--监听页面加载
    
    this.question(options.id);
                                  
     
  },
  
})